﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.MSTestV2.CLIAutomation;

namespace MSTest.IntegrationTests;

[TestClass]
public partial class TestId : CLITestBase
{
    private const string DefaultStrategyDll = "TestIdProject.DefaultStrategy";

    [TestMethod]
    public async Task TestIdUniqueness_DataRowArray_DefaultStrategy()
    {
        // Arrange
        string assemblyPath = GetAssetFullPath(DefaultStrategyDll);

        // Act
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~DataRowArraysTests");
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult> testResults = await RunTestsAsync(testCases);

        // Assert
        VerifyE2E.FailedTestCount(testResults, 0);
        VerifyE2E.TestsPassed(
            testResults,
            "DataRowArraysTests (0,[])",
            "DataRowArraysTests (0,[0])",
            "DataRowArraysTests (0,[0,0,0])");

        // We cannot assert the expected ID as it is path dependent
        CollectionAssert.AllItemsAreUnique(
            testResults.Select(x => x.TestCase.Id.ToString()).ToList(),
            "Test IDs should be unique.");
    }

    [TestMethod]
    public async Task TestIdUniqueness_DataRowString_DefaultStrategy()
    {
        // Arrange
        string assemblyPath = GetAssetFullPath(DefaultStrategyDll);

        // Act
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~DataRowStringTests");
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult> testResults = await RunTestsAsync(testCases);

        // Assert
        VerifyE2E.FailedTestCount(testResults, 0);
        VerifyE2E.TestsPassed(
            testResults,
            "DataRowStringTests (null)",
            "DataRowStringTests (\"\")",
            "DataRowStringTests (\" \")",
            "DataRowStringTests (\"  \")");

        // We cannot assert the expected ID as it is path dependent
        CollectionAssert.AllItemsAreUnique(
            testResults.Select(x => x.TestCase.Id.ToString()).ToList(),
            "Test IDs should be unique.");
    }

    [TestMethod]
    public async Task TestIdUniqueness_DynamicDataArrays_DefaultStrategy()
    {
        // Arrange
        string assemblyPath = GetAssetFullPath(DefaultStrategyDll);

        // Act
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~DynamicDataArraysTests");
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult> testResults = await RunTestsAsync(testCases);

        // Assert
        VerifyE2E.FailedTestCount(testResults, 0);
        VerifyE2E.TestsPassed(
            testResults,
            "DynamicDataArraysTests (0,[])",
            "DynamicDataArraysTests (0,[0])",
            "DynamicDataArraysTests (0,[0,0,0])");

        // We cannot assert the expected ID as it is path dependent
        CollectionAssert.AllItemsAreUnique(
            testResults.Select(x => x.TestCase.Id.ToString()).ToList(),
            "Test IDs should be unique.");
    }

    [TestMethod]
    public async Task TestIdUniqueness_DynamicDataTuple_DefaultStrategy()
    {
        // Arrange
        string assemblyPath = GetAssetFullPath(DefaultStrategyDll);

        // Act
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~DynamicDataTuplesTests");
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult> testResults = await RunTestsAsync(testCases);

        // Assert
        VerifyE2E.FailedTestCount(testResults, 0);
        VerifyE2E.TestsPassed(
            testResults,
            "DynamicDataTuplesTests ((1, text, True))",
            "DynamicDataTuplesTests ((1, text, False))");

        // We cannot assert the expected ID as it is path dependent
        CollectionAssert.AllItemsAreUnique(
            testResults.Select(x => x.TestCase.Id.ToString()).ToList(),
            "Test IDs should be unique.");
    }

    [TestMethod]
    public async Task TestIdUniqueness_DynamicDataGenericCollections_DefaultStrategy()
    {
        // Arrange
        string assemblyPath = GetAssetFullPath(DefaultStrategyDll);

        // Act
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~DynamicDataGenericCollectionsTests");
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult> testResults = await RunTestsAsync(testCases);

        // Assert
        VerifyE2E.FailedTestCount(testResults, 0);
        VerifyE2E.TestsPassed(
            testResults,
            "DynamicDataGenericCollectionsTests (System.Collections.Generic.List`1[System.Int32],System.Collections.Generic.List`1[System.String],System.Collections.Generic.List`1[System.Boolean])",
            "DynamicDataGenericCollectionsTests (System.Collections.Generic.List`1[System.Int32],System.Collections.Generic.List`1[System.String],System.Collections.Generic.List`1[System.Boolean])",
            "DynamicDataGenericCollectionsTests (System.Collections.Generic.List`1[System.Int32],System.Collections.Generic.List`1[System.String],System.Collections.Generic.List`1[System.Boolean])",
            "DynamicDataGenericCollectionsTests (System.Collections.Generic.List`1[System.Int32],System.Collections.Generic.List`1[System.String],System.Collections.Generic.List`1[System.Boolean])");

        // We cannot assert the expected ID as it is path dependent
        CollectionAssert.AllItemsAreUnique(
            testResults.Select(x => x.TestCase.Id.ToString()).ToList(),
            "Test IDs should be unique.");
    }

    [TestMethod]
    public async Task TestIdUniqueness_TestDataSourceArrays_DefaultStrategy()
    {
        // Arrange
        string assemblyPath = GetAssetFullPath(DefaultStrategyDll);

        // Act
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~TestDataSourceArraysTests");
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult> testResults = await RunTestsAsync(testCases);

        // Assert
        VerifyE2E.FailedTestCount(testResults, 0);
        VerifyE2E.TestsPassed(
            testResults,
            "Custom name",
            "Custom name",
            "Custom name");

        // We cannot assert the expected ID as it is path dependent
        CollectionAssert.AllItemsAreUnique(
            testResults.Select(x => x.TestCase.Id.ToString()).ToList(),
            "Test IDs should be unique.");
    }

    [TestMethod]
    public async Task TestIdUniqueness_TestDataSourceTuples_DefaultStrategy()
    {
        // Arrange
        string assemblyPath = GetAssetFullPath(DefaultStrategyDll);

        // Act
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~TestDataSourceTuplesTests");
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult> testResults = await RunTestsAsync(testCases);

        // Assert
        VerifyE2E.FailedTestCount(testResults, 0);
        VerifyE2E.TestsPassed(
            testResults,
            "Custom name",
            "Custom name");

        // We cannot assert the expected ID as it is path dependent
        CollectionAssert.AllItemsAreUnique(
            testResults.Select(x => x.TestCase.Id.ToString()).ToList(),
            "Test IDs should be unique.");
    }

    [TestMethod]
    public async Task TestIdUniqueness_TestDataSourceGenericCollections_DefaultStrategy()
    {
        // Arrange
        string assemblyPath = GetAssetFullPath(DefaultStrategyDll);

        // Act
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase> testCases = DiscoverTests(assemblyPath, "FullyQualifiedName~TestDataSourceGenericCollectionsTests");
        System.Collections.Immutable.ImmutableArray<Microsoft.VisualStudio.TestPlatform.ObjectModel.TestResult> testResults = await RunTestsAsync(testCases);

        // Assert
        VerifyE2E.FailedTestCount(testResults, 0);
        VerifyE2E.TestsPassed(
            testResults,
            "Custom name",
            "Custom name",
            "Custom name",
            "Custom name");

        // We cannot assert the expected ID as it is path dependent
        CollectionAssert.AllItemsAreUnique(
            testResults.Select(x => x.TestCase.Id.ToString()).ToList(),
            "Test IDs should be unique.");
    }
}
